<?php


class Image_Manager extends DB_Connect
{
    
    public $arrImg; //Noi luu tru anh
    public $maxInit;


    public function __construct($dbo = NULL) {
        parent::__construct($dbo);
        $this->arrImg = array();
        $this->maxInit =4;
    }
    
    public function addImage($id)
    {
        $arrImage = $this->_loadImage();
        if(is_array($arrImage))
        {
            foreach ($arrImage as $arrInfo) {
                
               $img = new Image($arrInfo); 
               array_push($this->arrImg, $img);
            }
        }        
        
    }
    
    public function _loadInit()
    {
        $sql = "SELECT 
                    `id`, `name`, `path`, 
                    `bshow`, `description` 
                FROM `gallery` LIMIT ";
        $sql .= $this->maxInit;
                  
        
        $stmt = $this->db->prepare($sql);         
        $stmt->execute(); 
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        $stmt->closeCursor(); 

        return $results; 
    }


    public function _loadImage($id=NULL)
    {
        
        //
        $sql = "SELECT 
                    `id`, `name`, `path`, 
                    `bshow`, `description` 
                FROM `gallery`";
        
        //Kiem tra cai nay la trong truong hop muon truy van ca table hoac chi lay 1 ID thoi
        if(!empty ($id))
            $sql .= "WHERE `id`=:id LIMIT 1";
        
        $stmt = $this->db->prepare($sql); 
        if ( !empty($id) ) 
        { 
            $stmt->bindParam(":id", $id, PDO::PARAM_INT); 
        }
        $stmt->execute(); 
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC); 
        $stmt->closeCursor(); 

        return $results;           
    }
    public function createMainView($path,$description)
    {
        $html = '<div id= "main_view" >';
        $img_html = '<img src="'.$path.'" width ="450" height ="400"/>';  
        $html .= $img_html;
        $html .= "</div>";
        
        echo $html;
    }
    
    public function createSlideBar()
    {
        $html = '<div id= "slide_bar" >';
        
       /* foreach ($this->arrImg as $image) {
            
            if(is_object($image) && get_class($image) == 'Image')
            {                      
                $title= 'Name:';
                $title.= $image->name.'<br />';
                
                $html_img = '<img src ="';
                $html_img.= $image->path;
                $html_img.= '" />';
                
                echo $title.$html_img;
            }
        }
        
        $html .= "</div>";*/
        $button_left = '<div id="btn_left"><img src="images/SlideControl/Left.png" /> </div>';
        $html .= $button_left;
        
         $button_Right = '<div id="btn_right"><img src="images/SlideControl/Right.png" /> </div>';
        
        $html .= $button_Right;
        $html .= '<div id= "Thumbnail" >';
        
        $arr_info_from_DB = $this->_loadImage();
                
        foreach ($arr_info_from_DB as $value) {
            
            $div_thumb = '<div class ="Thumb" >';
            $html .= $div_thumb;
            $path = $value['path'];
            $pos = strrpos($path, '/');
            $name_image = substr($path, $pos);           
            $img_html = '<img src="images/thumbSlide'.$name_image.'" width ="100" height ="100"/>';                  
            $html .= $img_html;
            $html .= '</div>';            
        }
                
        $html .= '</div>'; //end thumbnail
        
        $html .= '<div id = index >0</>';
        echo $html;
    }
    
    public function displaySlideshow()
    {     
              
        $path = 'images/thumbSlide/Me1.jpg';
        $desc ='This is demo for image';
        $this->createMainView($path,$desc);   
        
        $this->createSlideBar();
    }
}
?>
